using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.GridEX;
namespace Janus.NorthwindDemo2007
{
	public partial class frmSelectCustomer
	{
		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			NorthwindApp.MainForm.VisualStyleManager1.AddControl(this, true);
		}
		private void optAlphabetical_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.optAlphabetical.Checked)
			{
				this.GridEX1.RootTable.Groups.Clear();
				this.GridEX1.Focus();
			}
		}

		private void optCountry_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.optCountry.Checked)
			{
				this.GridEX1.RootTable.Groups.Add(new GridEXGroup(GridEX1.RootTable.Columns["Country"], Janus.Windows.GridEX.SortOrder.Ascending));
				this.GridEX1.FirstRow = 0;
				this.GridEX1.Row = 1;
				this.GridEX1.Focus();
			}
		}


		private void GridEX1_SelectionChanged(object sender, System.EventArgs e)
		{
			GridEXRow row = null;
			row = GridEX1.GetRow();
			if ((row != null) && row.RowType == RowType.GroupHeader)
			{
				GridEX1.Row = GridEX1.Row + 1;
			}
		}
		private void FillCountriesValueList()
		{
			DataView countries = new DataView(NorthwindApp.NorthwindApplication.GetCountriesTable());
			this.GridEX1.RootTable.Columns["Country"].HasValueList = true;
			this.GridEX1.RootTable.Columns["Country"].EditType = EditType.Combo;
			this.GridEX1.RootTable.Columns["Country"].ColumnType = ColumnType.ImageAndText;
			this.GridEX1.RootTable.Columns["Country"].ValueList.PopulateValueList(countries, "Country", "Country", "Flag", Color.Magenta, new Size(19, 13));
		}
		public NorthwindData.CustomersRow SelectCustomer(string customerID)
		{
			this.FillCountriesValueList();
			this.CustomersTableAdapter1.Fill(this.dsNorthwind.Customers);
			this.BindingContext = new BindingContext();
            this.GridEX1.DataSource = this.dsNorthwind.Customers;
			if (customerID.Length > 0)
			{
				this.GridEX1.Find(GridEX1.RootTable.Columns["CustomerID"], ConditionOperator.Equal, customerID, -1, 1);
			}
			else
			{
                this.GridEX1.Row = GridEX.emptyCurrentRow;                
                
			}
			if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				DataRowView rowView = (DataRowView)(GridEX1.GetRow().DataRow);
				return (NorthwindData.CustomersRow)rowView.Row;
			}
			else
			{
				return null;
			}
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			if (GridEX1.Row < 0)
			{
				MessageBox.Show("You must select a customer", NorthwindApp.MessageCaption);
				return;
			}
			this.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void GridEX1_DoubleClick(object sender, System.EventArgs e)
		{
			btnOK_Click(null, EventArgs.Empty);
		}

	}

} //end of root namespace